package com.taotao.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.taotao.pojo.TbContentCategory;
import com.taotao.service.TbContentCategoryService;

@RequestMapping(value = "content/category")
@Controller
public class ContentCategoryController {

	@Autowired
	private TbContentCategoryService service;

	@RequestMapping(method = RequestMethod.GET)
	public ResponseEntity<List<TbContentCategory>> getConCats(
			@RequestParam(value = "id", defaultValue = "0") Long id) {
		try {
			TbContentCategory cat = new TbContentCategory();
			cat.setParentId(id);
			List<TbContentCategory> cats = service.queryByWhere(cat);
			if (cats == null || cats.isEmpty()) {
				return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
			}
			return ResponseEntity.ok(cats);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(
				null);
	}

	/**
	 * 新增
	 * 
	 * @param cat
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST)
	public ResponseEntity<TbContentCategory> saveConCat(TbContentCategory cat) {
		try {
			cat.setId(null);
			cat.setStatus(1);
			cat.setIsParent(false);
			cat.setSortOrder(1);

			service.save(cat);

			TbContentCategory parent = service.queryById(cat.getParentId());
			if (!parent.getIsParent()) {
				parent.setIsParent(true);
				service.updateSelective(parent);
			}
			return ResponseEntity.status(HttpStatus.CREATED).body(cat);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(
				null);
	}

	/**
	 * 重命名
	 * 
	 * @param cat
	 * @return
	 */
	@RequestMapping(method = RequestMethod.PUT)
	public ResponseEntity<Void> updateConCat(TbContentCategory cat) {
		try {
			service.updateSelective(cat);

			return ResponseEntity.status(HttpStatus.NO_CONTENT).build();

		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}

	@RequestMapping(method = RequestMethod.DELETE)
	public ResponseEntity<Void> deleteConCat(TbContentCategory cat) {
		try {
			 List<Object> ids = new ArrayList<>();
			 ids.add(cat.getId());
			 findAllSubID(cat.getId(), ids);
             service.deleteByIds(TbContentCategory.class, "id", ids);
             TbContentCategory temp = new TbContentCategory();
             temp.setParentId(cat.getParentId());
             List<TbContentCategory> list = service.queryByWhere(temp);
             if(list == null || list.size() == 0){
            	 TbContentCategory parent = new TbContentCategory();
            	 parent.setId(cat.getParentId());
            	 parent.setIsParent(false);
            	 service.updateSelective(parent);
             }
			return ResponseEntity.status(HttpStatus.NO_CONTENT).build();

		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}

	private void findAllSubID(Long parentId, List<Object> ids){
		 
			TbContentCategory c = new TbContentCategory();
			c.setParentId(parentId);
			List<TbContentCategory> list = service.queryByWhere(c);
			
			for(TbContentCategory t : list){
				ids.add(t.getId());
				
				if(t.getIsParent()){
					findAllSubID(t.getId(),ids);
				}
			}
			
		 
	}
}
